<?php
/**
 * 模块-评论管理 
 * CommentController.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * -------------------------------
 * 
 * @package xcs
 * @author huangdaohai <huangdaohai.2007@163.com>
 * @version V1.0
 * @access public
 */
class CommentController extends CommentsController 
{
    protected $oModel;	
	public function __construct($id, $module) {
		$this->oModel = new ModuleCommentModel();
		return parent::__construct($id, $module);
	}
    /**
     * SiteController::actionIndex()
     * 
     * @return void
     */
    public function actionIndex() {
        $aParams['pagesize'] = $this->pagesize;
		$aParams['page'] = $this->_request->getQuery('page' ,1);
		$aResult = $this->oModel->getList($aParams);
        $aResult['params'] = $aParams;	
		$this->render('index', $aResult);
	}
    /**
     * CommentController::actionDetail() 评论详细
     * 
     * @return void
     */
    public function actionDetail(){
        $iCommentId = $this->_request->getQuery('comment_id' ,0);
        $oModel = $this->oModel->findByPk($iCommentId);
        $this->render('detail' ,array('aData'=>$oModel->attributes ,'aColumns'=>$this->oModel->getMetaData()->columns));
    }
    /**
     * ModuleController::actionAjaxCheck() 禁用启用模块
     * 
     * @return json 保存数据后结果
     */
    public function actionAjaxCheck(){
        $aResulte = array();
        $iCommentId = $this->_request->getQuery('comment_id' ,0);
        $oModel = ModuleCommentModel::model()->findByPk($iCommentId);
        if( ! empty( $oModel ) ){
            $oModel->status = $oModel->status==1 ? 2 : 1;
            $sDisable = $oModel->status==1 ? '未通过' : '通过';
            $html = "<a href='javascript:void(0)' class='ajaxOperateReplace' url='".$this->createUrl('/admin/comment/comment/ajaxCheck' ,array('comment_id'=>$iCommentId))."'>". $sDisable ."</a>";
            if( $oModel->save(false) ){
                $aResulte = array(
                    'err' => 0 ,
                    'msg' => '状态更新成功',
                    'data' => array('html'=>$html)
                );
            } else {
                $aResulte = array(
                    'err' => 1 ,
                    'msg' => '更新状态失败'
                );
            }
        } else {
            $aResulte = array(
                'err' => 1 ,
                'msg' => '信息错误，可能原因是数据已经被删除了.'
            );
        }
        die(json_encode($aResulte));
    }
    /**
     * SiteController::actionSetting()
     * 
     * @return void
     */
    public function actionSetting(){
        //获取评论设置
        $oModuleModel = BaseModule::model()->findByPk('comment');
        if( empty($oModuleModel) ){
            $alert="SendWebMsg('error','模块已经不存在了，请检查数据源')";
            //$alert="SendWebMsg('success','菜单添加成功','".$this->createUrl('module/index')."')";
        }
        $aPostData = $this->_request->getPost('setting');
        if( $aPostData ){
            $oModuleModel->setting = serialize($aPostData);
            $oModuleModel->updatetime = time();
            if( $oModuleModel->save(false) ){
                $alert="SendWebMsg('success','设置成功','".$this->createUrl('comment/setting')."')";
            } else {
                $alert="SendWebMsg('error','设置失败')";
            }
            Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
        }
        //处理数组
        $aSetting =  unserialize($oModuleModel->setting);
        if( ! isset($aSetting['isguest']) ) $aSetting['isguest']=0;
        if( ! isset($aSetting['ischeck']) ) $aSetting['ischeck']=0;
        if( ! isset($aSetting['iscode']) ) $aSetting['iscode']=0;
	    $this->render('_form',array('model'=>$this->oModel ,'setting'=>$aSetting));
	}
	/**
	 * CommentController::actionDelete()
	 * 
	 * @param mixed $id
	 * @return void
	 */
	public function actionDelete(){
	    $iCommentId = $this->_request->getQuery('comment_id');
		if( $this->oModel->deleteData($iCommentId) ){
            //同时删除回复的数据
            $this->oModel->deleteAll('pid='.$iCommentId);
			$this->ajaxReturn(0,'删除成功');
		} else {
			$this->ajaxReturn(1,'删除失败');
		}
	}
	/**
	 * CommentController::actionDeleteall()批量删除用户
	 * 
	 * @return void
	 */
	public function actionDeleteall(){
		$check=1;
		$ids=explode('|',$_POST['ids']);
		foreach ((array)$ids as $v){
			if( $this->oModel->deleteData($v) ){
			    //后台同时删除回复数据
				$this->oModel->deleteAll('pid='.$v);
			}else{
				$check=0;
			}
		}
		if($check){
			$this->ajaxReturn(0,'删除成功');
		}else{
			$this->ajaxReturn(1,'删除失败');
		}
	}
}